package demo.vo.tsdb;

import com.alibaba.fastjson2.JSON;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

/**
 * @author Stan.H
 */
public class DataPoint {

    private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy/MM/dd-HH:mm:ss");
    private long time;
    private double value;

    public DataPoint(long time, double value) {
        super();
        this.time = time;
        this.value = value;
    }

    public DataPoint() {
        super();
    }

    /**
     * 时间戳转时间
     *
     * @param timestamp
     * @return
     */
    public static LocalDateTime toLocalDateTime(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.of("+8"));
        return offsetDateTime.toLocalDateTime();
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public double getValue() {
        return value;
    }

    public void setValue(double data) {
        this.value = data;
    }

    public String getDate() {
        return DATE_FORMAT.format(toLocalDateTime(time));
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
